package com.surfilter.modules.role.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.surfilter.modules.role.model.ActionModel;
import com.surfilter.modules.role.model.ModuleModel;
import com.surfilter.modules.role.model.RoleModel;
import com.surfilter.modules.role.model.UserModel;
import com.surfilter.platform.base.dao.DaoBase;
import com.surfilter.platform.base.model.PageInfo;

@Repository
public class RoleDao extends DaoBase<RoleModel> {
	@Resource(name = "surfilterSqlMapClient")
	private SqlMapClient sqlMapClient;

	public int addRole(RoleModel roleModel) {
		int key = (Integer) this.addObject("addRole", roleModel);
		return key;
	}

	public RoleModel getRoleByName(RoleModel roleModel) {
		RoleModel rs = (RoleModel) this.getObjectByObject("getRoleByName",
				roleModel);
		return rs;
	}
	public void delRoles(List<Integer> ids){
		this.deleteObject("delRoles", ids);
	}
	public List<RoleModel> getRoles(RoleModel roleModel, PageInfo pageInfo) {
		List<RoleModel> retList = this.getObjectsByObjectForPage("getRoles", roleModel, pageInfo);
		for (RoleModel role : retList) {
			int id = role.getCreateUser();
			role.setCreateUserName((String)getObjectByObject("getUserNameById",id));
			id = role.getUpdateUser();
			if(id != 0){
				role.setUpdateUserName((String)getObjectByObject("getUserNameById",id));
			}
		}
		return retList;
	}

	public int getRolesCount(RoleModel model) {
		return (Integer) this.getObjectByObject("getRolesCount", model);
	}
	
	public void addAllRoleUsers(List<Integer> userIds,int roleId,int updateUser,Date createTime) {
		for (Integer integer : userIds) {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("userId", integer);
			params.put("roleId", roleId);
			params.put("createTime", createTime);
			this.addObject("addAllRoleUser", params);
		}
	}
	
	public void initSqlMapClient() {
		this.setSqlMapClient(sqlMapClient);
	}

	public RoleModel getRoleById(int roleId) {
		return (RoleModel) this.getObjectByObject("getRoleById", new RoleModel(roleId));
	}

	public void chgRole(RoleModel model, int roleId) {
		model.setId(roleId);
		this.updateObject("chgRole", model);
	}

	public void delRoleUsersByRoleID(int roleId) {
		this.deleteObject("delRoleUsersByRoleId", roleId);
	}
	
	public List<ModuleModel> getValidModulesByRole(RoleModel roleModel) {
		return this.getListByObject("getValidModulesByRoleByRoleModule", roleModel);
	}
	
	public List<ModuleModel> getAllValidModules() {
		List<ModuleModel> moduleModels = this.getListByObject(
				"getAllValidModulesByRoleModule", null);
		return moduleModels;
	}
	
	public List<ActionModel> getValidActionsByRole(RoleModel roleModel) {
		return this.getListByObject("getActionsByRoleByRoleModule", roleModel);
	}
	
	public List<ActionModel> getActionsOfModule(int id) {
		return this.getListByObject("getActionsOfModuleByRoleModule", id);
	}
	
	public List<UserModel> getUsersByRoleId(int id) {
		return this.getListByObject("getUsersByRoleId", id);
	}
	
	public List<UserModel> getExtraUsersByRoleId(int id) {
		return this.getListByObject("getExtraUsersByRoleId", id);
	}
	
}
